<?php 
include('../conn.php');
/* orderData:[{shopid:'店铺id',goods:[{goodsid:'商品id',addnum:'购买数量',carid:'购物车id,直接购买就为空字符串'}]}]
 */
	

	//收货地址id
	$addressid = '';	
	//用户id
	$userid = '';
	//店铺和商品信息
	$orderData = '';

	//最终返回数据
	$resultArray = array();
	//收货地址信息
	$address_info = array();
	//所有商家信息
	$all_shop_info_data = array();

	if(empty($_REQUEST['userid'])){
		$resultArray['code'] = -100;
		$resultArray['msg'] = '用户信息异常0';
		echo json_encode($resultArray);
		exit;
	}else{
		$userid = $_REQUEST['userid'];
	}

	if(empty($_REQUEST['orderData'])){
		$resultArray['code'] = -100;
		$resultArray['msg'] = '商品信息异常0';
		echo json_encode($resultArray);
		exit;
	}else{
		//转成关联数组
		$orderData = json_decode($_REQUEST['orderData'],true);
		if(!$orderData){
			$resultArray['code'] = -100;
			$resultArray['msg'] = '商品信息异常1';
			echo json_encode($resultArray);
			exit;

		}
	}

	if(isset($_REQUEST['addressid'])){
		$addressid = $_REQUEST['addressid'];
	}

	$user_query = mysql_query('select return_volume from user where userid = '.$userid);
	$user_result = mysql_fetch_assoc($user_query);
	if(!$user_result){
		$resultArray['code'] = -100;
		$resultArray['msg'] = '用户信息异常';
		echo json_encode($resultArray);
		exit;

	}
	
	// 查找送货地址
	$addressid_query = '';
	if ($addressid != '') {
		$addressid_query = mysql_query("select * from address where id=".$addressid);
	}else{
		// 默认地址
		$addressid_query = mysql_query("select * from address where userid=".$userid." and state = 1");
		// 默认地址 end
	}
	$myaddress = mysql_fetch_assoc($addressid_query);
	if($myaddress){
		$address_info['code'] = 200;
		$address_info['data'] = $myaddress;
		$address_info['msg'] = '查询到默认收货地址';
	}else{
		$address_info['code'] = -100;
		$address_info['msg'] = '没有查询到默认收货地址';
	}

	//orderData:[{shopid:'店铺id',goods:[{goodsid:'商品id',addnum:'购买数量',carid:'购物车id,直接购买就为空字符串'}]}]
	foreach ($orderData as $key => $orderData_item) {
		//d店铺id
		$shopid = $orderData_item['shopid'];
		//商品数组
		$goods_list = $orderData_item['goods'];


		//一个商家的商品总的快递费
		$total_freightRisk = 0;
		//一个商家的商品总的运费险
		$total_Freight = 0;

		//如果没有传店铺id,或者不是数字
		if(!$shopid || !is_numeric($shopid)){
			$resultArray['code'] = -100;
			$resultArray['msg'] = '商家信息异常2';
			echo json_encode($resultArray);
			exit;

		}
		//如果没有购买该店铺的商品，则异常
		if(!$goods_list || !is_array($goods_list) || count($goods_list) == 0){
			$resultArray['code'] = -100;
			$resultArray['msg'] = '商品信息异常2';
			echo json_encode($resultArray);
			exit;

		}

		$shop_query = mysql_query("select freight_ins,name,shopid from shop where shopid=".$shopid);
		//得到一个商家的信息
		$shop_info_data = mysql_fetch_assoc($shop_query);
		if(!$shop_info_data){
			$resultArray['code'] = -100;
			$resultArray['msg'] = '商家信息异常3';
			echo json_encode($resultArray);
			exit;
		}

		//循环计算商品信息
		foreach ($goods_list as $k => $goods) {
			$goodsid = $goods['goodsid'];
			$addnum = $goods['addnum'];
			$carid = $goods['carid'];
			//如果没有传商品id、购买数量，或者不是数字
			if(!$goodsid || !is_numeric($goodsid) || !$addnum || !is_numeric($addnum)){
				$resultArray['code'] = -100;
				$resultArray['msg'] = '商品信息异常3';
				echo json_encode($resultArray);
				exit;

			}

			//如果有购物车id,但不是数字
			if($carid != '' && !is_numeric($carid) ){
				$resultArray['code'] = -100;
				$resultArray['msg'] = '商品信息异常4';
				echo json_encode($resultArray);
				exit;

			}

			//查询商品信息
			$goods_query = mysql_query("select goodsid,muid,zixilie,shopid from goods where goodsid = ".$goodsid);
			$goods_query_info = mysql_fetch_assoc($goods_query);
			if(!$goods_query_info){
				$resultArray['code'] = -100;
				$resultArray['msg'] = '商品信息异常5';
				echo json_encode($resultArray);
				exit;

			}

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$goods_query_info['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
	
			$result0 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$goods_query_info['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result1 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$goods_query_info['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result2 = mysql_fetch_assoc($sql);


			$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$goods_query_info['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result3 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$goods_query_info['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result4 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$goods_query_info['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result5 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$goods_query_info['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result6 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$goods_query_info['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result7 = mysql_fetch_assoc($sql);
			
			if ($result0) {
				$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result1) {
				$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result2) {
				$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result3){
				$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result4) {
				$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = 0)";
			}elseif ($result5) {
				$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = 0)";
			}elseif ($result6) {
				$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = 0)";
			}else{
				$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = 0)";
			}
			// 运费条件 end
			
			// 计算运费 
				
			$sql = mysql_query("select * from fare  WHERE muid = ".$goods_query_info['muid']."  ".$where_area);
			$fare_row  = mysql_fetch_assoc($sql);
			if($fare_row){
				$fare = $fare_row['fare'];
				$addfare = $fare_row['addfare'];
				$begincount = $fare_row['begincount'];
				$distance_count = $fare_row['distance_count'];

				//加上初始件数运费
				
				$fare+=0;
				$begincount+=0;
				$distance_count+=0;
				$total_freightRisk += $fare;
				//购买的件数大于初始件数
				

				if($addnum > $begincount){
					//计算超过件数
					$distance_num = $addnum - $begincount;
					//计算需要加多少次，向下取整
					$addfare_count = floor($distance_num/$distance_count);

					$total_freightRisk += $addfare_count*$addfare;
				}
			}
				
			// 计算运费 end
			

			// 计算运费 end

			// // 计算运费险
			$query = mysql_query("select * from freightrisk");
			$freightRisk = 0;
			while ($row = mysql_fetch_assoc($query)) {
				if ($addnum >= $row[goodsNum]) {
					$freightRisk = $row[freightRisk];
				}
			}
			$total_Freight += $freightRisk;
			

		}


		$shop_info_data['freightRisk'] = $total_Freight;
		$shop_info_data['distribution_fee'] = $total_freightRisk;
		$all_shop_info_data[] = $shop_info_data;


	}
	$resultArray['code'] = 200;
	$resultArray['msg'] = '获取成功';
	$resultArray['address_info'] = $address_info;
	$resultArray['shopInfo'] = array('code'=>200,'data'=>$all_shop_info_data);
	$resultArray['return_volume'] = $user_result['return_volume']+0;
	echo json_encode($resultArray);




















 ?>